/**
 * Created by LeungZ on 2016/12/21.
 */
import addTmp from './form/add.html';
import addCtrl from './form/add.ctrl.js';

import delTmp from './form/delete.html';
import delCtrl from './form/delete.ctrl.js';

class Group {
    constructor($uiModal, list, nodeSrv, $stateParams, ctgNotification, $scope) {
        "ngInject";
        this._uiModal = $uiModal;
        this._nodeSrv = nodeSrv;
        this._notification = ctgNotification;
        this._emit = $scope.$emit.bind($scope);
        this.dbCode = $stateParams.kDbCode;
        this.checkedId = '';

        this.page = {
            currentPage: 1,
            totalItems: 0
        };
        this.filter = {
            name: null
        };

        if (list) {
            this.table = list.list;
            this.notNextPage = !list.hasNextPages;
            this.notPreviousPage = !list.hasPreviousPages;
        } else {
            this.table = [];
            this.notNextPage = true;
            this.notPreviousPage = true;
        }
    }

    search() {
        this.page.currentPage = 1;
        this.table = null;
        this.getList();
    }

    groupChecked(elem) {
        if (this.checkedId == elem.groupId) return;

        this.checkedId = elem.groupId;
        this._emit('searchByGroup', elem.groupCode);
    }

    nextPage() {
        if (this.notNextPage) return;

        this.page.currentPage++;
        this.table = null;
        this.notNextPage = true;
        this.notPreviousPage = true;
        this.getList();
    }

    prevPage() {
        if (this.notPreviousPage) return;

        this.page.currentPage--;
        this.table = null;
        this.notNextPage = true;
        this.notPreviousPage = true;
        this.getList();
    }

    getList() {
        this._nodeSrv.getGroupList({code: this.dbCode, page: this.page.currentPage}, this.filter).then(data=> {
            this.table = data.list;
            this.page.totalItems = data.total;
            this.notNextPage = !data.hasNextPages;
            this.notPreviousPage = !data.hasPreviousPages;
        }).catch(data=> {
            if (!this.table) {
                this.table = [];
                this.notNextPage = true;
                this.notPreviousPage = true;
            }
            this._notification.error('获取宿主机组详情数据有误.');
        })
    }

    openGroupFrom(elem) {
        this._uiModal.open({
            templateUrl: addTmp,
            controller: addCtrl,
            controllerAs: 'vm',
            backdrop: 'static',
            size: 'lg',
            resolve: {
                group: function () {
                    if (elem) {
                        return {
                            id: elem.groupId,
                            name: elem.groupCode
                        }
                    } else return null
                }
            }
        }).result.then(()=> {
                this.getList();
            })
    }

    deleteGroup(elem) {
        this._uiModal.open({
            templateUrl: delTmp,
            controller: delCtrl,
            controllerAs: 'vm',
            windowClass: 'alert-dialog alert-dialog-ctg',
            resolve: {
                groupCode: function () {
                    return elem.groupCode;
                }
            }
        }).result.then(()=> {
                elem.oprLoading = true;
                this._nodeSrv.deleteGroup(elem.groupId).then(data=> {
                    if (this.table.length == 1 && this.page.currentPage > 1) this.page.currentPage--;

                    this.getList();
                    this._notification.success('删除宿主机组 ' + elem.groupCode + '成功.');
                }).catch(data=> {
                    this._notification.error('删除宿主机组 ' + elem.groupCode + '失败. ' + data.resultMsg);
                }).finally(()=> {
                    elem.oprLoading = false;
                })
            });

    }
}
export default Group